﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DataClass;
using System.Data;

namespace BUS
{
    public class CatalogueBase
    {
        public static string SaveData(string[] storeName, DataSet ds)
        {
            string strReturn = "0";
            using (DataConnect objConn = new DataConnect())
            {
                objConn.Open();
                objConn.BeginTransaction();
                //SqlTransaction trans = objConn.BeginTransaction();
                try
                {
                    for (int idxTable = 0; idxTable < ds.Tables.Count; idxTable++)
                    {
                        string[] strParamNames = new string[ds.Tables[idxTable].Columns.Count];
                        object[] objParamValues = new object[ds.Tables[idxTable].Columns.Count];
                        for (int idxRows = 0; idxRows < ds.Tables[idxTable].Rows.Count; idxRows++)
                        {
                            for (int idxColumn = 0; idxColumn < ds.Tables[idxTable].Columns.Count; idxColumn++)
                            {
                                strParamNames[idxColumn] = ds.Tables[idxTable].Columns[idxColumn].ColumnName;
                                objParamValues[idxColumn] = ds.Tables[idxTable].Rows[idxRows][idxColumn];
                            }
                            objConn.SaveData(storeName[idxTable], strParamNames, objParamValues);
                        }
                    }
                    //trans.Commit();
                    objConn.CommitTransaction();
                }
                catch (Exception ex)
                {
                    //trans.Rollback();
                    objConn.RollbackTransaction();
                    if (ex.GetType().Name == typeof(SqlException).Name)
                        strReturn = "Exception Number : " + ((SqlException)ex).Number + "\n OnProc : " + ((SqlException)ex).Procedure 
                            + "\n Server : " + ((SqlException)ex).Server + "\n Message : " + ex.Message 
                            + "\n StackTrace : " + ((SqlException)ex).StackTrace;
                    else
                        strReturn = ex.Message + "@" + ex.StackTrace;
                }
                finally
                {
                    objConn.Close();
                }
            }
            return strReturn;
        }

        public static string SaveData(string storeName, string[] strParam,object[] objValue)
        {
            string strReturn = "0";
            using (DataConnect objConn = new DataConnect())
            {
                objConn.Open();
                //SqlTransaction trans = objConn.BeginTransaction();
                objConn.BeginTransaction();
                try
                {
                    objConn.SaveData(storeName, strParam, objValue);
                    //trans.Commit();
                    objConn.CommitTransaction();
                }
                catch (Exception ex)
                {
                    //trans.Rollback();
                    objConn.RollbackTransaction();
                    if (ex.GetType().Name == typeof(SqlException).Name)
                        strReturn = "Exception Number : " + ((SqlException)ex).Number + "\n OnProc : " + ((SqlException)ex).Procedure
                            + "\n Server : " + ((SqlException)ex).Server + "\n Message : " + ex.Message
                            + "\n StackTrace : " + ((SqlException)ex).StackTrace;
                    else
                        strReturn = ex.Message + "@" + ex.StackTrace;
                }
                finally
                {
                    objConn.Close();
                }
            }
            return strReturn;
        }

        public static DataSet GetData(string storeName, string[] strParam, object[] objParam)
        {
            DataSet ds = new DataSet();
            using (DataConnect objConn = new DataConnect())
            {
                objConn.Open();
                try
                {
                    ds = objConn.GetData(storeName, strParam, objParam);
                }
                catch (Exception ex)
                {
                    if (ex.GetType().Name == typeof(SqlException).Name)
                        ds.DataSetName = "Exception Number : " + ((SqlException)ex).Number + "\n OnProc : " + ((SqlException)ex).Procedure
                            + "\n Server : " + ((SqlException)ex).Server + "\n Message : " + ex.Message
                            + "\n StackTrace : " + ((SqlException)ex).StackTrace;
                    else
                        ds.DataSetName = ex.Message + "@" + ex.StackTrace;
                }
                finally
                {
                    objConn.Close();
                }
            }
            return ds;
        }

        public static string DeleteData(string storeName, string[] strParam, object[] objValue)
        {
            string result = "0";
            using (DataConnect objConn = new DataConnect())
            {
                objConn.Open();
                //SqlTransaction trans = objConn.BeginTransaction();
                objConn.BeginTransaction();
                try
                {
                    objConn.DeleteData(storeName, strParam, objValue);

                    //trans.Commit();
                    objConn.CommitTransaction();
                }
                catch (Exception ex)
                {
                    objConn.RollbackTransaction();
                    if (ex.GetType().Name == typeof(SqlException).Name)
                        result = "Exception Number : " + ((SqlException)ex).Number + "\n OnProc : " + ((SqlException)ex).Procedure
                            + "\n Server : " + ((SqlException)ex).Server + "\n Message : " + ex.Message
                            + "\n StackTrace : " + ((SqlException)ex).StackTrace;
                    else
                        result = ex.Message + "@" + ex.StackTrace;
                }
                finally
                {
                    objConn.Close();
                }
            }
            return result;
        }

        public static bool CheckConnection()
        {
            try
            {
                using (DataConnect objConn = new DataConnect())
                {
                    return objConn.CheckConnection();
                }
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}
